import Vue from 'vue';
import Vuex from 'vuex';
import { findNodePath } from '@/plugins/utils';
Vue.use(Vuex);
const store = new Vuex.Store({
  state: {
    menuList: [
      {
        pid: 0,
        id: 1,
        title: "系统管理",
        url: "/system",
        type: 1,
        children: [
          {
            pid: 1,
            id: 2,
            title: "角色管理",
            url: "/role",
            type: 2,

          },
          {
            pid: 1,
            id: 3,
            title: "账号管理",
            url: "/account",
            type: 2,
          },
        ],
      },
      {
        pid: 0,
        id: 4,
        title: "商城管理",
        url: "/shop",
        type: 1,
        children: [
          {
            pid: 4,
            id: 5,
            title: "商品管理",
            url: "/goods",
            type: 2,
          }
        ]
      }
    ],
  },
  mutations: {
    // 添加菜单
    menuAdd(state, payload) {
      // 随机生成菜单id
      const id = Date.now();
      if (payload.pid == 0) {
        payload.children = [];
        state.menuList.push({ ...payload, id });
      } else {
        const index = state.menuList.findIndex(item => item.id === payload.pid);
        state.menuList[index].children = state.menuList[index].children ? state.menuList[index].children : [];
        state.menuList[index].children.push({ ...payload, id });
      }
    },
    // 编辑菜单
    menuUpdate(state, payload) {
      // 根据菜单id搜索树形数组, 找到要更新的菜单
      const menu = findNodePath(state.menuList, payload.id);
      if (menu) {
        if (menu.type == 1) {
          const index = state.menuList.findIndex(item => item.id == menu.id);
          // 删除原来的菜单
          state.menuList.splice(index, 1);
        } else {
          // 查找父级菜单索引
          const pIndex = state.menuList.findIndex(item => item.id == menu.pid);
          // 查找待更新二级菜单索引
          const sIndex = state.menuList[pIndex].children.findIndex(item => item.id === menu.id);
          // 删除原来二级菜单
          state.menuList[pIndex].children.splice(sIndex, 1);
        }
      }
      // 在适当的位置再添加以一条菜单数据
      if (payload.pid == 0) {   // 添加为一级菜单即可
        state.menuList.push({ ...payload });
      } else {
        // 根据pid查找父级菜单索引
        const parentIndex = state.menuList.findIndex(item => item.id === payload.pid);
        // 在父级菜单中添加一条二级菜单
        state.menuList[parentIndex].children = state.menuList[parentIndex].children ? state.menuList[parentIndex].children : [];
        state.menuList[parentIndex].children.push({ ...payload });
      }
    },
    // 删除菜单
    menuDel(state, payload) {
      // 一级菜单
      if (payload.type == 1) {
        const index = state.menuList.findIndex(item => item.id == payload.id);
        state.menuList.splice(index, 1);
      } else {  // 二级菜单
        // 查找父级菜单索引
        const pIndex = state.menuList.findIndex(item => item.id == payload.pid);
        // 查找待删除菜单索引
        const sIndex = state.menuList[pIndex].children.findIndex(item => item.id === payload.id);
        // 删除二级菜单
        state.menuList[pIndex].children.splice(sIndex, 1);
      }
    }
  }
});
export default store;